package com.guanyong.admin.controllers.api;

import com.guanyong.dao.IPriceTagDaoMapper;
import com.guanyong.model.PriceTagModel;
import com.guanyong.services.PriceTagService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.utils.UniqueID.DBObjectID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/5/31.
 */
@RestController
@RequestMapping("/api/price/")
public class PriceTagApi {
    @Autowired
    PriceTagService priceTagService;

    @RequestMapping(value = "pricelist", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getpricetaglist(HttpServletRequest request,
                                                         @RequestParam String name,

                                                     @RequestParam int pno,
                                                     @RequestParam int pagesize,
                                                     @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();

        HashMap<String,Object> hashMap=priceTagService.GetPriceTagList(name,pno,
                pagesize,allcount);
        result.setResult(hashMap);
        return result;
    }

    @RequestMapping(value = "getpriceall", method = RequestMethod.GET)
    ResultObject<List<PriceTagModel>> getpriceall(HttpServletRequest request) {
        ResultObject<List<PriceTagModel>> result = new ResultObject<List<PriceTagModel>>();

        List<PriceTagModel> list=priceTagService.GetAll();
        result.setResult(list);
        return result;
    }
    @RequestMapping(value = "pricetagadd", method = RequestMethod.POST)
    ResultObject<Integer> AddPriceTag(
            @RequestParam("pricetag_name") String pricetag_name,
            @RequestParam("pricetag_money") Double pricetag_money,
            @RequestParam("pricetag_coin") Double pricetag_coin,
            @RequestParam("pricetag_offday") Integer pricetag_offday,
            @RequestParam("pricetag_desc") String pricetag_desc,
            HttpServletRequest request) {

        PriceTagModel priceTagModel=new PriceTagModel();
        String newid = new DBObjectID().toString();
        priceTagModel.setfId(newid);
        priceTagModel.setfCoin(pricetag_coin);
        priceTagModel.setfDesc(pricetag_desc);
        priceTagModel.setfMoney(pricetag_money);
        priceTagModel.setfName(pricetag_name);
        priceTagModel.setfOffday(pricetag_offday);
        ResultObject<Integer> result = new ResultObject<Integer>();

        int res=priceTagService.AddPriceTag(priceTagModel);
        if (res >-1){
            result.setCode(ResultCode.OperateOk);
        }
        else{
            result.setCode(ResultCode.OperateFail);
        }
        return  result;
    }


    @RequestMapping(value = "pricetagupdate", method = RequestMethod.POST)
    ResultObject<Integer> UpdatePriceTag(
            @RequestParam("pricetag_id") String pricetag_id,
            @RequestParam("pricetag_name") String pricetag_name,
            @RequestParam("pricetag_money") Double pricetag_money,
            @RequestParam("pricetag_coin") Double pricetag_coin,
            @RequestParam("pricetag_offday") Integer pricetag_offday,
            @RequestParam("pricetag_desc") String pricetag_desc,
            HttpServletRequest request) {

        PriceTagModel priceTagModel=new PriceTagModel();
        priceTagModel.setfId(pricetag_id);
        priceTagModel.setfCoin(pricetag_coin);
        priceTagModel.setfDesc(pricetag_desc);
        priceTagModel.setfMoney(pricetag_money);
        priceTagModel.setfName(pricetag_name);
        priceTagModel.setfOffday(pricetag_offday);
        ResultObject<Integer> result = new ResultObject<Integer>();

        int res=priceTagService.UpdatePriceTag(priceTagModel);
        if (res >-1){
            result.setCode(ResultCode.OperateOk);
        }
        else{
            result.setCode(ResultCode.OperateFail);
        }
        return  result;
    }
}
